package danling

import (
	"encoding/base64"
	"encoding/json"
	"gitee.com/thingple/detonator/rest"
	"github.com/lishimeng/go-log"
)

type QbqProjectInfo struct {
	No           string `json:"no,omitempty"`           // 起爆器编号
	Manufacturer string `json:"manufacturer,omitempty"` // 起爆器厂商
	UnitCode     string `json:"unitCode,omitempty"`     // 单位代码(使用单位)
	UnitName     string `json:"unitName,omitempty"`     // 单位名称(使用单位)
	SerialNo     string `json:"serialNo,omitempty"`     // 设备序列号
}

func (c *client) RegisterQbq(projects ...QbqProjectInfo) (code int, err error) {

	req := Req{}
	req.Xpch = c.xpch

	for _, project := range projects {
		req.Projects = append(req.Projects, project)
	}

	var resp Resp

	bs, err := json.Marshal(req)
	if err != nil {
		return
	}
	//tmp := strings.ReplaceAll(string(bs), " ", "")
	log.Info("req:%s", bs)
	reqBs, err := encode(bs, c.desKey)
	if err != nil {
		return
	}
	reqContent := base64.StdEncoding.EncodeToString(reqBs)
	log.Info("req:%s", reqContent)
	params := make(map[string]string)
	params["param"] = reqContent
	code, err = rest.NewRest(c.host).Path("qbqxxServlet", "addQbqxx").ResponseJson(&resp).FormUrl(params)
	if err != nil {
		return
	}
	return
}
